package com.hisense.hiioc.formtable.handler;

import com.hisense.hiioc.formtable.util.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.zip.DataFormatException;

/**
 * 全局异常捕获处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * http请求的方法不正确
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public ResponseResult httpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e) {
        logger.error("http请求的方法不正确:【"+e.getMessage()+"】");
        return ResponseResult.Builder.buildError("http请求的方法不正确");
    }

    /**
     * 请求参数不全
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public ResponseResult missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException e) {
        logger.error("请求参数不全:【"+e.getMessage()+"】");
        return ResponseResult.Builder.buildError("请求参数不全");
    }

    /**
     * 请求参数类型不正确
     */
    @ExceptionHandler(TypeMismatchException.class)
    @ResponseBody
    public ResponseResult typeMismatchExceptionHandler(TypeMismatchException e) {
        logger.error("请求参数类型不正确:【"+e.getMessage()+"】");
        return ResponseResult.Builder.buildError("请求参数类型不正确");
    }

    /**
     * 数据格式不正确
     */
    @ExceptionHandler(DataFormatException.class)
    @ResponseBody
    public ResponseResult dataFormatExceptionHandler(DataFormatException e) {
        logger.error("数据格式不正确:【"+e.getMessage()+"】");
        return ResponseResult.Builder.buildError("数据格式不正确");
    }

    /**
     * 非法输入
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public ResponseResult illegalArgumentExceptionHandler(IllegalArgumentException e) {
        logger.error("非法输入:【"+e.getMessage()+"】");
        return ResponseResult.Builder.buildError("非法输入");
    }

    @ExceptionHandler  //处理其他异常
    @ResponseBody
    public Object allExceptionHandler(Exception e, HttpServletRequest request, HttpServletResponse response){
        System.out.println(e.getStackTrace());
        logger.error("具体错误信息:【"+e.getMessage()+"】"); //会记录出错的代码行等具体信息
        e.printStackTrace();
        if(isAjax(request)) {
            return ResponseResult.Builder.buildError(e.getMessage());
        } else {
            ModelAndView mav = new ModelAndView();
            mav.addObject("exception",e);
            mav.addObject("url", request.getRequestURL());
            mav.setViewName("error");
            return mav;
        }
    }


    /**
     * 判断是否ajax请求
     * @param httpRequest
     * @return
     */
    public static boolean isAjax(HttpServletRequest httpRequest){
        return (httpRequest.getHeader("X-Requested-With") != null
                && "XMLHttpRequest".equals( httpRequest.getHeader("X-Requested-With").toString()) );
    }

}
